﻿Namespace Load

    ''' <summary>
    ''' 列エイリアスファクトリー（リレーションテーブル）
    ''' </summary>
    ''' <remarks></remarks>
    Friend Class RelationTableColumnAliasFactory
        Inherits RootTableColumnAliasFactory

        ''' <summary>
        ''' テーブルエイリアスから列エイリアスを作成します
        ''' </summary>
        ''' <param name="als"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shadows Function Create(als As RelationTableAlias) As List(Of ColumnAlias)
            Dim lst As New List(Of ColumnAlias)
            Dim i As Integer = 0

            For Each item As ColumnAttribute In (From el In als.Table.Columns Where el.IsLoadable)
                Dim col As ColumnAttribute = item
                Dim rel As IParentRelation = (From el In als.Relations Where el.YourColumnName = col.ColumnName).FirstOrDefault
                If rel Is Nothing Then
                    '結合条件でない場合は、通常処理
                    lst.Add(Me.CreateColumnAlias(als, item, i))
                Else
                    '結合条件の場合は、リレーション用処理
                    lst.Add(Me.CreateColumnAlias(als, item, rel))
                End If
                i += 1
            Next

            Return lst
        End Function

        ''' <summary>
        ''' 列エイリアスを作成します（リレーション用）
        ''' </summary>
        ''' <param name="als"></param>
        ''' <param name="col"></param>
        ''' <param name="rel"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Private Overloads Function CreateColumnAlias(als As TableAlias, col As ColumnAttribute, rel As IParentRelation) As ColumnAlias
            Dim c As ColumnAlias = Me.CreateColumnAlias(als, col, 0)

            c.BaseColumnAlias = (From el In als.BaseTableAlias.ColumnAliases Where el.Column.ColumnName = rel.MyColumnName).First

            Return c
        End Function

    End Class

End Namespace
